package edu.uwosh.cs342.project3;

import edu.uwosh.cs342.project3.Login;
import edu.uwosh.cs342.project3.DBHelper;
import java.util.ArrayList;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Startup extends Activity {

	public String user = "", pass = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.startup);
		createLogin();

	}

	/**
	 * Creates login dialog screen for user.
	 */
	public void createLogin() {
		final Dialog dialog = new Dialog(Startup.this);
		dialog.setContentView(R.layout.login);
		dialog.setTitle("Login");

		dialog.setCancelable(true);
		dialog.show();

		Button login = (Button) (dialog.findViewById(R.id.login));

		login.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Login i = new Login();
				DBHelper dbh = new DBHelper();
				EditText username = (EditText) (dialog
						.findViewById(R.id.username));
				EditText password = (EditText) (dialog
						.findViewById(R.id.password));
				user = username.getText().toString();
				pass = password.getText().toString();

				if (user.isEmpty() || pass.isEmpty()) {
					Toast.makeText(Startup.this, "Blank Fields",
							Toast.LENGTH_LONG).show();
				} 
				else {
					i.setUsername(user);
					i.setPassword(pass);
					if (dbh.validate(i) == 'I') {
						Toast.makeText(Startup.this, "Invalid login ",
								Toast.LENGTH_LONG).show();
					} else {

						Toast.makeText(Startup.this, "Successful Login ",
								Toast.LENGTH_LONG).show();
						dialog.cancel();

						Intent ourIntent = new Intent(Startup.this,
								MainActivity.class);
						startActivity(ourIntent);
					}
				}
			}
		});
	}
}
